home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / STATS.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  2KB  |  54 lines

  1. /****************************************************************/
  2. /*                                                              */
  3. /*      Collect file statistics                                 */
  4. /*                                                              */
  5. /*      Public domain demo program for analyzing encrypted      */
  6. /*      files. By: Bob Stout                                    */
  7. /*                                                              */
  8. /****************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include <assert.h>
  14.  
  15. main(int argc, char *argv[])
  16. {
  17.       int i, ch, hist = 0;
  18.       long n = 0L;
  19.       double mean = 0., stdev = 0., ftmp;
  20.       static unsigned bins[256];
  21.       FILE *infile;
  22.  
  23.       assert(infile = fopen(argv[1], "rb"));
  24.       while (!feof(infile))
  25.       {
  26.             if (EOF == (ch = fgetc(infile)))
  27.                   break;
  28.             bins[ch] += 1;
  29.             ++n;
  30.       }
  31.       fclose(infile);
  32.       for (i = 0; i < 256; ++i)
  33.       {
  34.             mean += (double)(bins[i]);
  35.             if (bins[i])
  36.                   ++hist;
  37.       }
  38.       mean /= 256.;
  39.       for (i = 0; i < 256; ++i)
  40.       {
  41.             ftmp = (double)(bins[i]) - mean;
  42.             stdev += (ftmp * ftmp);
  43.       }
  44.       ftmp  = stdev / 255.;
  45.       stdev = sqrt(ftmp);
  46.       printf("%ld Characters were read from %s\n"
  47.             "There are an average of %f occurances of each character\n"
  48.             "%d Characters out of 256 possible were used\n"
  49.             "The standard deviation is %f\n"
  50.             "The coefficient of variation is %f%%\n",
  51.             n, argv[1], mean, hist, stdev, (100. * stdev) / mean);
  52.       return EXIT_SUCCESS;
  53. }
  54.